<template>
  <div class="login-register">
    <div class="contain">
      <div class="big-box" :class="{ active: isLogin }">
        <div v-show="isLogin" class="big-contain">
          <div class="login-form-wrap">
            <div class="login-head">
              <div class="login-logo" />
            </div>
            <el-form
              ref="loginFormRef"
              :rules="loginFormRules"
              :model="loginForm"
            >
              <el-form-item prop="teacherId">
                <el-input
                  v-model="loginForm.teacherId"
                  placeholder="请输入工号"
                />
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  v-model="loginForm.password"
                  placeholder="请输入密码"
                  show-password
                />
              </el-form-item>
              <el-form-item>
                <el-button class="login-btn" type="primary" @click="login">
                  登录
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
        <div v-show="!isLogin" class="big-contain">
          <div class="regist-form-wrap">
            <div class="regist-head">
              <div class="rigist-logo" />
            </div>
            <el-form
              ref="registerFormRef"
              :rules="registerFormRules"
              :model="registerForm"
            >
              <el-form-item prop="teacherId">
                <el-input
                  v-model="registerForm.teacherId"
                  placeholder="请输入工号"
                />
              </el-form-item>
              <el-form-item prop="username">
                <el-input
                  v-model="registerForm.username"
                  placeholder="请输入姓名"
                />
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  v-model="registerForm.password"
                  placeholder="请输入密码"
                  show-password
                />
              </el-form-item>
              <el-form-item prop="email">
                <el-input
                  v-model="registerForm.email"
                  placeholder="请输入邮箱"
                />
              </el-form-item>
              <el-form-item prop="">
                <el-input
                  v-model="registerForm.code"
                  placeholder="请输入验证码"
                />
                <a
                  href="javascript:"
                  type="button"
                  class="get-check-code"
                  @click="getCheckCode()"
                >{{ codeText }}</a>
              </el-form-item>
              <el-form-item>
                <el-button class="login-btn" type="primary" @click="register">
                  注册
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <div class="small-box" :class="{ active: isLogin }">
        <div v-if="isLogin" class="small-contain">
          <div class="stitle">
            你好，朋友!
          </div>
          <p class="scontent">
            没有账号，立刻注册 ~
          </p>
          <button class="sbutton" @click="changeType">
            注册
          </button>
        </div>
        <div v-else class="small-contain">
          <div class="stitle">
            拥有账户!
          </div>
          <p class="scontent">
            请立刻登录 ~
          </p>
          <button class="sbutton" @click="changeType">
            登录
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import { checkEmail, checkTeacherId } from '@/utils/checkRules.js'
import { login, register, getCheckCode } from '@/api/teacherApi'
export default {
  data() {
    return {
      isLogin: true,
      // 登录表单
      loginForm: {
        teacherId: '',
        password: ''
      },
      // 注册表单
      registerForm: {
        teacherId: '',
        username: '',
        password: '',
        email: '',
        code: ''
      },
      // 登录表单验证规则
      loginFormRules: {
        teacherId: [
          { required: true, message: '请输入教师工号', trigger: 'blur' },
          { validator: checkTeacherId, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3~10 个字符之间', trigger: 'blur' }
        ]
      },
      // 注册表单验证规则
      registerFormRules: {
        teacherId: [
          { required: true, message: '请输入教师工号', trigger: 'blur' },
          { validator: checkTeacherId, trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3~10 个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3~10 个字符之间', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ]
      },
      sending: false, // 是否发送验证码
      second: 60, // 倒计时间
      codeText: '获取验证码'
    }
  },
  methods: {
    // 改变当前操作类型，是注册操作还是登录操作
    changeType() {
      this.isLogin = !this.isLogin
      this.$refs.loginFormRef.resetFields()
      this.$refs.registerFormRef.resetFields()
      this.registerForm.code = ''
    },
    // 登录用户
    login() {
      // 表单验证
      this.$refs.loginFormRef.validate(async(valid) => {
        if (valid) {
          const { data: res } = await login(this.loginForm)
          // 存储 token
          Cookies.set('token', res.token, { expires: 1 / 24 / 2 })
          // 存储 教师工号
          Cookies.set('teacherId', this.loginForm.teacherId, { expires: 1 / 24 / 2 })
          this.$message.success('登录成功')
          this.$router.push('/layout')
        }
      })
    },
    // 注册用户
    register() {
      // 表单验证
      this.$refs.registerFormRef.validate(async valid => {
        if (!valid) return
        // 校验 code 是否为全数字并且为长度为 6
        if (this.registerForm.code.length !== 6 || !/^\d{6}$/.test(this.registerForm.code)) { return this.$message.error('验证码格式有误') }
        // 注册用户
        await register(this.registerForm)
        this.$message.success('注册成功')
        this.isLogin = true
      })
    },
    // 获取验证码
    getCheckCode() {
      // 校验表单
      this.$refs.registerFormRef.validate(async valid => {
        if (!valid) return
        if (this.sending) this.sending = true
        this.timeDown()
        await getCheckCode(this.registerForm.email)
        this.$message.success('发送成功')
      })
    },
    // 倒计时
    timeDown() {
      const result = setInterval(() => {
        this.codeText = this.second
        this.second--
        if (this.second < 1) {
          clearInterval(result)
          this.sending = false
          this.second = 60
          this.codeText = '获取验证码'
        }
      }, 1000)
    }
  }
}
</script>

<style scoped>
.login-register {
  width: 100vw;
  height: 100vh;
  box-sizing: border-box;
  background: url("https://yuan-exp-schedule-file.oss-cn-beijing.aliyuncs.com/imgs/login_bg.jpg") no-repeat;
  background-size: cover;
}
.contain {
  width: 50%;
  height: 65%;
  position: relative;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 20px;
  box-shadow: 0 0 3px #f0f0f0, 0 0 6px #f0f0f0;
}
.big-box {
  width: 70%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 30%;
  transform: translateX(0%);
  transition: all 1s;
}
.big-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: skyblue;
}
.btitle {
  font-size: 1.5em;
  font-weight: bold;
  color: rgb(57, 167, 176);
}
.bform {
  width: 100%;
  height: 40%;
  padding: 2em 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.bform .errTips {
  display: block;
  width: 50%;
  text-align: left;
  color: red;
  font-size: 0.7em;
  margin-left: 1em;
}
.bform input {
  width: 50%;
  height: 30px;
  border: none;
  outline: none;
  border-radius: 10px;
  padding-left: 2em;
  background-color: #f0f0f0;
}
.bbutton {
  width: 20%;
  height: 40px;
  border-radius: 24px;
  border: none;
  outline: none;
  background-color: rgb(57, 167, 176);
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}
.small-box {
  width: 30%;
  height: 100%;
  background: linear-gradient(135deg, rgb(57, 167, 176), rgb(56, 183, 145));
  position: absolute;
  top: 0;
  left: 0;
  transform: translateX(0%);
  transition: all 1s;
  border-top-left-radius: inherit;
  border-bottom-left-radius: inherit;
}
.small-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.stitle {
  font-size: 1.5em;
  font-weight: bold;
  color: #fff;
}
.scontent {
  font-size: 0.8em;
  color: #fff;
  text-align: center;
  padding: 2em 4em;
  line-height: 1.7em;
}
.sbutton {
  width: 60%;
  height: 40px;
  border-radius: 24px;
  border: 1px solid #fff;
  outline: none;
  background-color: transparent;
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}

.big-box.active {
  left: 0;
  transition: all 0.5s;
}
.small-box.active {
  left: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-top-right-radius: inherit;
  border-bottom-right-radius: inherit;
  transform: translateX(-100%);
  transition: all 1s;
}
.login-form-wrap {
  min-width: 300px;
  padding: 30px 50px 30px;
}
.login-head {
  display: flex;
  justify-content: center;
}
.login-logo {
  height: 130px;
  width: 300px;
  background: url("https://yuan-exp-schedule-file.oss-cn-beijing.aliyuncs.com/imgs/logo_index.png") no-repeat;
  background-size: contain;
}
.regist-form-wrap {
  min-width: 300px;
  padding: 30px 50px 30px;
}
.regist-head {
  display: flex;
}
.rigist-logo {
  height: 57px;
  width: 200px;
  background: url("https://yuan-exp-schedule-file.oss-cn-beijing.aliyuncs.com/imgs/logo_index.png") no-repeat;
  background-size: contain;
  margin:0 auto 10px auto;
}
.login-btn {
  width: 100%;
}
.el-form-item {
  margin-bottom: 35px;
}
.regist-form-wrap .el-form-item {
  margin-bottom: 20px;
}
.get-check-code {
  color: #424242;
  position:absolute;
  right: 10px;
}
</style>
